package com.copa.ums.modules.ums.controller;


import com.copa.ums.common.api.CommonPage;
import com.copa.ums.common.vo.ResponseVo;
import com.copa.ums.modules.ums.model.UmsMenu;
import com.copa.ums.modules.ums.model.UmsResource;
import com.copa.ums.modules.ums.model.UmsRole;
import com.copa.ums.modules.ums.service.UmsRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author copa
 * @since 2022-10-31
 */
@RestController
@Api(tags = "用户角色管理")
@Tag(name = "用户角色管理", description = "用户角色管理")
@RequestMapping("role")
public class UmsRoleController {

    @Resource
    private UmsRoleService roleService;

    @ApiOperation("添加角色")
    @PostMapping("create")
    public ResponseVo<String> create(@RequestBody UmsRole role) {
        return roleService.create(role);
    }

    @ApiOperation("修改角色")
    @RequestMapping(value = "/update/{roleId}", method = RequestMethod.POST)
    public ResponseVo<String> update(@PathVariable("roleId") Long roleId,
                                     @RequestBody UmsRole role) {
        role.setId(roleId);
        return roleService.updateById(role)
                ? ResponseVo.success(null)
                : ResponseVo.failed();
    }

    @ApiOperation("批量删除角色")
    @PostMapping("delete")
    public ResponseVo<String> delete(@RequestParam("roleIds") List<Long> roleIds) {
        return roleService.delete(roleIds);
    }

    @ApiOperation("获取所有角色")
    @GetMapping("listAll")
    public ResponseVo<List<UmsRole>> listAll() {
        return ResponseVo.success(roleService.list());
    }

    @ApiOperation("根据角色名称分页获取角色列表")
    @GetMapping("list")
    public ResponseVo<CommonPage<UmsRole>> list(@RequestParam(value = "keyword", required = false) String keyword,
                                                @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        return roleService.list(keyword, pageSize, pageNum);
    }

    @ApiOperation("修改角色状态")
    @PostMapping("updateStatus/{roleId}")
    public ResponseVo<String> updateStatus(@PathVariable("roleId") Long roleId,
                                           @RequestParam("status") Integer status) {
        UmsRole umsRole = new UmsRole();
        umsRole.setId(roleId);
        umsRole.setStatus(status);
        return roleService.updateById(umsRole)
                ? ResponseVo.success(null)
                : ResponseVo.failed();
    }

    @ApiOperation("获取角色相关菜单")
    @GetMapping("listMenu/{roleId}")
    public ResponseVo<List<UmsMenu>> listMenu(@PathVariable("roleId") Long roleId) {
        return roleService.listMenu(roleId);
    }

    @ApiOperation("获取角色相关资源")
    @GetMapping(value = "/listResource/{roleId}")
    public ResponseVo<List<UmsResource>> listResource(@PathVariable Long roleId) {
        return roleService.listResource(roleId);
    }

    @ApiOperation("给角色分配菜单")
    @PostMapping("allocMenu")
    public ResponseVo<Integer> allocMenu(@RequestParam("roleId") Long roleId,
                                         @RequestParam("menuIds") List<Long> menuIds) {
        return roleService.allocMenu(roleId, menuIds);
    }

    @ApiOperation("给角色分配资源")
    @PostMapping("allocResource")
    public ResponseVo<Integer> allocResource(@RequestParam("roleId") Long roleId,
                                             @RequestParam("resourceIds") List<Long> resourceIds) {
        return roleService.allocResource(roleId, resourceIds);
    }
}

